package player.ui.video;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.border.Border;

import player.PS;
import player.PlayerStart;


public class VideoLayout implements LayoutManager{
	VideoCanvas canvas;
	static Rectangle rec;
	VideoLayout(){
		super();
	}
	public void addLayoutComponent(String arg0, Component arg1) {
		
	}

	public void removeLayoutComponent(Component c) {
		
	}
	
	public void layoutContainer(Container c) {
		int count = c.getComponentCount();
	    for (int i = 0 ; i < count ; i++) {
	        Component m = c.getComponent(i);
	        if(m.getName()!=null){
	        	if(m.getName().equals("videoCanvas")){
		        	canvas=(VideoCanvas)m;
		        	setCanvas();
		        	
		        }
	        }else{
	        	  Rectangle rec=new Rectangle(100,100,200,300);
	  	        m.setBounds(rec);
	        }
	    }
	}
	
	void setCanvas(){
		
		float cx, cy = 0, cw, ch;
		Container cp=(Container) canvas.getParent();
		
		rec=new Rectangle();
		if(PS.videoFrame.isFullSereen){
			ch = cp.getBounds().height;
			cw = ch * PS.videoRate ;
			cx = (cp.getBounds().width - cw) / 2;
			if (cw > cp.getBounds().width) {
				cx = 0;
				cw = cp.getBounds().width;
				ch = cw /PS.videoRate ;
			}
			cy = (cp.getBounds().height - ch) / 2;
		}else{
			Insets gap=cp.getInsets();
			ch = cp.getBounds().height-(gap.top+gap.bottom);
			cw = cp.getWidth()-(gap.left+gap.right) ;
			cx=gap.left;
			cy=gap.top;
		}
		rec=new Rectangle((int) cx, (int) cy, (int) cw, (int) ch+1);
		new Thread(){
			public void run(){
				canvas.setBounds(rec);
			}
		}.start();
	}
	
	public Dimension minimumLayoutSize(Container c) {
		Dimension dm=new Dimension(0,0);
		return dm;
	}

	public Dimension preferredLayoutSize(Container c) {
		Dimension dm=new Dimension(50,50);
		return dm;
	}

	
	
	void setMiniPlayer(){
		
	}
	
	
	
	void layOut(){
		
	}


}
